<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>智慧写作平台</title>
<link href="/pad/resources/css/bootstrap.min.css" rel="stylesheet">
<link href="./resources/css/star-rating.min.css" rel="stylesheet">
<link href="/pad/resources/css/style.css" rel="stylesheet">
</head>
<body>
	<nav class="navbar navbar-blue navbar-fixed-top" role="navigation">
		<div class="container bs-docs-container">
			<div class="navbar-header">
				<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-6">
					<span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span>
				</button>
				<h1>智慧写作平台</h1>
			</div>
			<ul class="nav navbar-nav">
				<li class="active"><a href="#">课程列表</a></li>
			</ul>
			
			<ul class="nav navbar-nav navbar-right">
				<li class="dropdown"><a id="user" href="#" class="dropdown-toggle" data-toggle="dropdown">返回<span class="caret"></span></a>
					<ul class="dropdown-menu" role="menu">
						<li><a href="/pad/logout">退出登录</a></li>
					</ul></li>
			</ul>
		</div>
	</nav>
	<div class="container bs-docs-container">
		<div>
			<div id="pingfen"></div>
			<iframe id="editpad" src='#' width="100%" height="300" frameborder="no" border="0"></iframe>
		</div>
	</div>
	<script src="/pad/resources/js/jquery.min.js"></script>
		 <div id="cprtFooter">
	        <!-- 版权信息 -->
	 </div>	

<script type="text/javascript">
		$("#cprtFooter").load("copyrightFooter.html");
</script>
	<script src="/pad/resources/js/jquery.form.min.js"></script>
	<script src="/pad/resources/js/bootstrap.min.js"></script>
	<script src="/pad/resources/js/star-rating.min.js"></script>
	<script>
	/*$(function(){
		$.ajax({
			url:'/pad/comment/saveAssignmentId?assignmentId=' + getQueryString('assignmentId'),
			success : function() { console.log("发送成功");},
			error:function(){console.log("发送失败 ")}
		})
 	});*/
 	    function getAsId(){
 		 var assignment_Id;
 		 var activity_Id;
  	     assignment_Id=getQueryString('assignmentId')
  	     activity_Id=getQueryString('activityId')
  	     
  	     window.location.href="/pad/textAnalyse.html?assignmentId="+assignment_Id+"&activityId="+activity_Id
 	    }
 	   
 	
 	    var queList;// 维度  
 		document.cookie="activityId="+getQueryString('activityId')+";path=/";
		function getQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg);
			var groupid;
			if (r != null)
				return unescape(r[2]);
			return null;
		}
		$.ajax({
			url : '/pad/getSessionUser',
			success : function(user) {
				$.ajax({
					url : '/pad/assignment/getAssignmentPadCurrentId?assignmentId='
							+ getQueryString('assignmentId'),
					success : function(data) {
						var info = data.id.split("$");
						groupid = info[0];
						$("#editpad").attr("height", $(window).height() - 130);
						$("#editpad").attr(
								"src",
								"http://localhost:8081/p/" + data.id
										+ "?userName=" + user.realname);
						
						//明早直接在这儿传参数 获取信息显示所有人的评论按钮
						$.ajax({
							url : '/pad/comment/getCommentQuestions?assignmentId='+getQueryString('assignmentId'),
							success : function(data) {
								var htmlStr = "";
								var userList = data.comment;
								for(var i in userList){
									htmlStr += '<button class="btn btn-info" data-toggle="modal" data-target="#commentModal'+i+'">给'+userList[i].userName+'评分</button> ';
									commentModal("commentModal"+i,user.id,data.assignmentPadId,userList[i]);//产生评分 名字 按钮 并调用 下面写的  commentModal 方法
								}
								$('#pingfen').html(htmlStr);
							}
						});
					}
				});
				$("#user").html(user.realname);
			}
		});
		function commentModal(modalId,commenterId,assignmentPadId,user){
			var commentModal ='<div id="'+modalId+'" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">';
			commentModal +='<div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>';
			commentModal +='<h4 class="modal-title" id="myModalLabel">给'+user.userName+'评分</h4></div><div class="modal-body">';
			commentModal +='<h5 class="modal-title" id="myModalLabel">请根据你在小组内相处的经验，从1到5的范围描述你的看法，“一颗星”到“五颗星”分别表示非常不同意，比较不同意，一般，比较同意与非常同意，对应得分依次为1—5分</h5></div><div class="modal-body">';
			var questionList = user.commentQuestion;
			//拿取 问卷数据
			var ques;//接收问卷数据
			$.ajax({
				   async: false,	
				   url: "./resources/json/questionnaire.json",//json文件位置
				   type: "GET",//请求方式为get
				   dataType: "json", //返回数据格式为json
				   success: function(data) {//请求成功完成后要执行的方法 
					   questionnaire=data.questionnaire;
		           }
	});        
			
			
			for(var i in questionList){
				for(var j=0;j<questionnaire.length;j++){
					if(questionnaire[j].dim==questionList[i].question){
						commentModal +='<form role="form" action="/pad/comment/saveOrUpdateComment">';
						commentModal +='<input type="hidden" name="questionId" value="'+questionList[i].id+'">';
						commentModal +='<input type="hidden" name="commenterId" value="'+commenterId+'">';
						commentModal +='<input type="hidden" name="assignmentPadId" value="'+assignmentPadId+'">';
						commentModal +='<input type="hidden" name="authorId" value="'+user.userId+'">';
						commentModal +='<input type="hidden" name="dim" value="'+questionnaire[j].dim+'">';//增加  归属维度  例如问题  属于 影响力范畴
						commentModal +='<div class="form-group">';
						//commentModal +='<label>'+questionList[i].question+'</label>';
						commentModal +='<label>'+questionnaire[j].question+'</label>';
						commentModal +='<input value="0" name="score" type="number" class="'+modalId+' rating form-control" min="0" max="'+questionnaire[j].score+'" step="1" data-stars="'+questionnaire[j].score+'" style="display: none;">';
						commentModal +='</div>';
						commentModal +='</form>';
					}
				}
				
			}
			commentModal +='</div>';
			commentModal +='<div class="modal-footer">';
			commentModal +='<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>';
			commentModal +='<button type="button" class="btn btn-info" onclick="submitForm(\'#'+modalId+'\');">提交</button>';
			commentModal +='</div>';
			commentModal +='</div>';
			commentModal +='</div>';
			commentModal +='</div>';
			var assignmentModal = $("#"+modalId);
			if(assignmentModal.length>0)
				assignmentModal.replaceWith(commentModal);
			else
				$("html").append(commentModal);
			$("."+modalId).rating({
				step:1
			});
		}
		function submitForm(modalSelecter){
			// 异步提交表单
			var count=0;  //自己提取 组装一个  comment对象   authorid  commenterid  score 
			var questionList;
			$.ajax({
				   async: false,	
				   url: "./resources/json/questionnaire.json",//json文件位置
				   type: "GET",//请求方式为get
				   dataType: "json", //返回数据格式为json
				   success: function(data) {//请求成功完成后要执行的方法 
					   questionList=data.questionDims;
		           }
	});        
			
			for(var i in questionList){
                var scoreCount=0;//按照维度提交了几次				
				var sumScore=0;
				var count=0;//个数
				var score=0;//维度 得分 平均值结果
			    var comment={};//提交到 后台的 json 数据
			    var first=true;//是不是   遍历form 时的 第一个form
			$(modalSelecter).find('form').each(function(){
				
				if(first==true){
					
					comment["commenterId"]=this.commenterId.value;//打分的人
					comment["assignmentPadId"]=this.assignmentPadId.value;
					comment["authorId"]=this.authorId.value;// 把之前备份项目页面 替换过来  做测试  记下 提交到后台的 数据  格式 
					//自己封装数据类型   关键 要卡 comment 的   id  是怎么处理的
					first=false;
				}
				
				if(questionList[i].name==new String(this.dim.value).toString()){
					comment["questionId"]=this.questionId.value;
					sumScore+=Number(this.score.value);
					count++;
				}
				
			});
				if(count>0){
					score=sumScore/count;
				}
				
				comment["score"]=score.toString(); 
				 $.ajax({
					   url: "/pad/comment/saveOrUpdateComment",
					   type: 'POST',
					   data:comment,
					   dataType: "json", //返回数据格式为json
					   success: function(data) {//请求成功完成后要执行的方法 
						   if(data=='success'){
				        		if(scoreCount==0){
				        		alert("打分成功！");
				        		scoreCount++;
				        		}
				        		$(modalSelecter).modal('hide');
				        	}else{
				        		alert(data);
				        	}
				        }
		           });         
				
			}//按照问题维度 提交表单   有几个维度 提交几次表单   求出维度平均值 
		}
	</script>
</body>
</html>
